﻿#include "YToolWidget.h"
#include <QApplication>
#include <QScreen>
#include <QTimer>
// add by liq 12
#include <QDesktopWidget>
// add by liq test
#include <QMessageBox>

YToolWidget::YToolWidget(QWidget *parent) : QWidget(parent)
{
//    desktopSize = qApp->primaryScreen()->size();
    // liq test
    QScreen* desktop = QGuiApplication::primaryScreen();
    desktopSize = desktop->size();
    setWindowFlags(Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
//    setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_AlwaysShowToolTips);
//    setAttribute(Qt::WA_ShowWithoutActivating, true);

    // del by liq 12
    // raise会导致QToolTip被遮挡，此timer是为了绘图时使工具栏一直显示，考虑其他绘图为何会不显示，此处暂且注掉
//    QTimer *timer = new QTimer(this);
//    timer->setTimerType(Qt::TimerType::VeryCoarseTimer);
//    connect(timer, &QTimer::timeout, [this]{ timeout(); });
//    timer->start(20);
}

// add by liq 12
void YToolWidget::showTool(){
    show();
}
void YToolWidget::raiseTool(){
    raise();
}
void YToolWidget::hideTool(){
    hide();
}
void YToolWidget::onReferRectChanged(const QRect &rect){
    referRect_ = rect;
    adjustPos();
}

void YToolWidget::adjustPos()
{
    int x = referRect_.x() + referRect_.width() - this->width();
    int y = referRect_.y() + referRect_.height() + 5;
    if (x + this->width() > QApplication::desktop()->rect().width()) {
        x = QApplication::desktop()->rect().width() - this->width();
    }
    if (y + this->height() > QApplication::desktop()->rect().height()) {
//        y = referRect_.y() - this->height() - 2;
        int testY = referRect_.y() - 5 - this->height();
        if(testY >= 0) y = testY; else y = 0;
        toped = true;
    }else{
        toped = false;
    }
//    x = qMax(x, 2);
//    y = qMax(y, 2);

    // add by liq 16
    // 防止在屏幕最左侧时工具栏偏离出屏幕，使其左侧对齐选区
    if(x < QApplication::desktop()->pos().x()){
        x = referRect_.x();
    }

    this->move(x, y);

//    QMessageBox mb(this);
//    QString width_str = QString::number(pDeskp.x(), 10);
//    QString height_str = QString::number(x, 10);
//    char* width_char;
//    char* height_char;
//    QByteArray width_qba = width_str.toLatin1();
//    QByteArray height_qba = height_str.toLatin1();
//    width_char = width_qba.data();
//    height_char = height_qba.data();
//    //设置文本框的大小
//    mb.setStyleSheet("QLabel{"
//                      "min-width: 300px;"
//                      "min-height: 150px; "
//                      "font-size:12px;"
//                      "}");
//    mb.setText(QString::fromLocal8Bit(width_char) + "|" + QString::fromLocal8Bit(height_char));
//    mb.setWindowTitle(QString::fromLocal8Bit("提示"));
//    mb.move(500,300);
//    mb.exec();
}

// 录屏时工具栏位置设置
void YToolWidget::set(const QRect &rect, const QRect& originScreenRect)
{
//    int x = (rect.x() + width() > desktopSize.width() ? desktopSize.width() - width() : rect.x());
    int x = (rect.x() + width() > originScreenRect.width() ? originScreenRect.width() - width() : rect.x());
    int y = rect.bottom() + 5;

//    if(y + height() > desktopSize.height()){
    // 减掉系统任务栏高度40
    if(y + height() > originScreenRect.height() - 40){
        int testY = rect.y() - 5 - height();
        if(testY >= 0){
            y = testY;
        }else{
//            y = 0;
            // 为0时会压线，此时显示在录屏区域内，但会工具栏会被录制到视频中
            y = rect.y() + 2;
        }
        toped = true;
    }else{
        toped = false;
    }

    move(x, y);

    if(isHidden()) show();
    raise();
}

void YToolWidget::set(const QPoint &pos, const QRect& originScreenRect, const QSize &size)
{
    if(size.isValid()){
        set(QRect(pos, size), originScreenRect);
    }else{
        set(QRect(pos.x(), 0, 1, pos.y()), originScreenRect);
    }
}

void YToolWidget::unset()
{
    if(!isHidden()) {
        hide();
    }
}

void YToolWidget::timeout()
{
    // del by liq 12
    // raise会导致QToolTip被遮挡，此timer是为了绘图时使工具栏一直显示，考虑其他绘图为何会不显示，此处暂且注掉
//    if(isVisible()) raise();
}
void YToolWidget::mousePressEvent(QMouseEvent *e){
//    if(isVisible()) raise();

}
